// ==========================================================================
// GEA自动生成控制器相关代码，只生成一次，按需修改,再次生成不会覆盖.
// 生成日期：{{.table.CreateTime}}
// 生成路径: app/system/{{.table.ModuleName}}/internal/api/{{.table.BusinessName}}.go
// 生成人：{{.table.FunctionAuthor}}
// ==========================================================================
package api

import (
    "{{.table.PackageName}}/app/system/{{.table.ModuleName}}/internal/define"
    "{{.table.PackageName}}/app/system/{{.table.ModuleName}}/internal/service"
    "{{.table.PackageName}}/app/utils/response"
    "github.com/gogf/gf/net/ghttp"
)

var {{.table.BusinessName | CamelString}} = new({{.table.BusinessName | CamelLower}}Api)

type {{.table.BusinessName | CamelLower}}Api struct {
    BaseController
}

func (a *{{.table.BusinessName | CamelLower}}Api) Init(r *ghttp.Request) {
    a.Module = "{{.table.FunctionName}}管理"
}

// Get 列表
func (a *{{.table.BusinessName | CamelLower}}Api) Get(r *ghttp.Request) {
    var req *define.{{.table.BusinessName | CamelString}}ApiSelectPageReq
    //获取参数
    if err := r.Parse(&req); err != nil {
	    a.Err(r,err.Error())
    }
    result := service.{{.table.BusinessName | CamelString}}.GetList(req)
    {{if eq .table.TplCategory "tree"}}
    if result != nil {
        a.Succ(r, result)
    } else {
        a.Succ(r, 0, nil)
    }
    {{else}}
    if result != nil {
        a.SuccTable(r, result.Total, result.List)
    } else {
        a.SuccTable(r, 0, nil)
    }
    {{end}}
}


// Post 新增
func (a *{{.table.BusinessName | CamelLower}}Api) Post(r *ghttp.Request) {
    var req *define.{{.table.BusinessName | CamelString}}ApiCreateReq
    //获取参数
    if err := r.Parse(&req); err != nil {
      a.Err(r,err.Error())
    }
    id, err := service.{{.table.BusinessName | CamelString}}.Create(r.Context(),req)
    if err != nil {
        a.Err(r, err.Error())
    }
    if  id <= 0 {
        a.Err(r, "保存失败")
    }
    a.Succ(r, id)
}

// Put 修改
func (a *{{.table.BusinessName | CamelLower}}Api) Put(r *ghttp.Request) {
    var req *define.{{.table.BusinessName | CamelString}}ApiUpdateReq
    //获取参数
    if err := r.Parse(&req); err != nil {
        a.Err(r, err.Error())
    }
    rs, err := service.{{.table.BusinessName | CamelString}}.Update(r.Context(),req)
    if err != nil {
        a.Err(r, err.Error())
    }
    if  rs <= 0 {
        a.Err(r, "修改失败")
    }
    a.Succ(r, rs)
}

// Delete 删除
func (a *{{.table.BusinessName | CamelLower}}Api) Delete(r *ghttp.Request) {
    var req *define.{{.table.BusinessName | CamelString}}ApiDeleteReq
    //获取参数
    if err := r.Parse(&req); err != nil {
        a.Err(r, err.Error())
    }
    rs := service.{{.table.BusinessName | CamelString}}.Delete(req.Ids)
    if rs > 0 {
        a.Succ(r, rs)
    } else {
        a.Err(r, "删除失败")
    }
}

// Info 详情
func (a *{{.table.BusinessName | CamelLower}}Api) Info(r *ghttp.Request) {
    id := r.GetInt64("id")
    if id <= 0 {
        a.Err(r,"参数错误")
    }
    entity, err := service.{{.table.BusinessName | CamelString}}.Info(id)
    if err != nil || entity == nil {
        a.Err(r,"数据不存在")
    }
    a.Succ(r,entity)
}

// Export 导出
func (a *{{.table.BusinessName | CamelLower}}Api) Export(r *ghttp.Request) {
    r.SetCtxVar(response.ResponseBunissType,response.Buniss_Export)
    var req *define.{{.table.BusinessName | CamelString}}ApiSelectPageReq
    //获取参数
    if err := r.Parse(&req); err != nil {
        a.Err(r,err.Error())
    }
    url, err := service.{{.table.BusinessName | CamelString}}.Export(req)
    if err != nil {
        a.Err(r,err.Error())
    }
    a.Succ(r,url)
}